﻿jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var cdnurl = 'http://hua-static.fengqiaoju.com';

$(document).ready(function(){
	//增加访客数
	addView();
	
	setTimeout("$('.pic_focus .focus').css('max-height','');",500);
	
	//点击底部搜索框
	$('.home-search-value').focus(function(){
		$("html,body").animate({scrollTop:$(".home-search-value").offset().top},1);	
		$('#footer').hide();
	}).blur(function(){
		$('#footer').css('bottom',0).show();
		$("html,body").animate({scrollTop:$(".home-search-value").offset().top},1);
	});
	
	//评论
	$('.home-comment-input').focus(function(){
		$(this).css('height','100px');									
	}).blur(function(){
		$(this).css('height','30px');
	});
	$('.home-comment-btn').click(function(){
		var url = '/?c=Ajax&a=comment&do=add';
		var obj = $('.home-comment-form');
		var context = obj.find('[name=context]').val();
		var obj_id = obj.find('[name=obj_id]').val();
		var obj_type = obj.find('[name=obj_type]').val();
		var data = {'obj_type':obj_type,'obj_id':obj_id,'context':context};
		obj.find('span').html('<img src="'+cdnurl+'/home/images/loading.gif" />');
		obj.find('[name=context]').val('');
		obj.find('[name=context]').attr('placeholder','提交中...');
		$.post(url,data,function(rs){
			if(rs.status == 1){
				obj.find('[name=context]').attr('placeholder','请输入评论内容');
				var html = '<li><div class="fl"> <span><img src="'+cdnurl+'/home/images/0.jpg" width="50" height="50" class="home-comment-btn" /></span><b>您</b><p>'+context+'</p></div> <div class="fr">刚刚</div></li>	';	
				if($('.home-comment-list').html().trim() == '暂无评论'){
					$('.home-comment-list').html('');
				}
				$('.home-comment-list').prepend(html);
				obj.find('[name=context]').val('');
				obj.find('span').html(rs.msg);
				setTimeout("$('.home-comment-form').find('span').html('');",5000);
			}else{
				obj.find('span').html(rs.msg).css('color','red');
			}
		});												
	});		

	//搜索
	$('.home-search-btn').click(function(){
		var keyword = $('.home-search-value').val();
		if(!keyword){
			alert('请输入花卉名字');
			return false;
		}
		location.href = '/name/'+keyword+'/?mobile=1';
	});

	$("#bts").toggle(function(){	  
		  $("#nav").slideDown('fast');
		},function(){
		  $("#nav").slideUp('fast');	
	});
	
	
	$('.user-menu li').on('click',function(){
		var left = $('.user-item').offset().left;
		
		//当前对象
		var nowId = $('.user-menu li.selected').data('id');
		var nowObj = '#'+nowId;		
		
		//切换导航颜色
		$('.user-menu li').removeClass('selected');
		$(this).addClass('selected');
		
		//切换内容
		var id = $(this).data('id');
		var obj = '#'+id;
		
		if(nowObj == obj){
			return ;
		}
		$('.user-item').find(nowObj).hide();
		$('.user-item').find(obj).show();
	});
	
	var fh = $("#footer").height();
	$(".full").height(fh);
	$(".nd-nav,#user").css("bottom",fh+"px");
	
	Y('G',function(){
		var titles = document.querySelectorAll('.title_focus a'),
			spans = document.querySelectorAll('.select_focus span'),
			c = 0,
			focus = ScrollImg();
		
		focus = Swipe(document.querySelector('.pic_focus'),{
			 auto : 2000,
			 disableScroll: false,
			 stopPropagation: false,
			 callback : set
		})
		function set(n){
			titles[c].className = '';
			spans[c].className = '';
			c = n;
			titles[c].className = 'on';
			spans[c].className = 'on';
		}
	});
	if (document.referrer.indexOf('fengqiaoju')<0){
		$(".top-home .bt01").css("background-image","url(/Public/mobile/images/bt01no.jpg?1)").attr("onClick","");
    }else{
		$(".top-home .bt01").css("background-image","url(/Public/mobile/images/bt01.jpg?1)").attr("onClick","window.history.go(-1)");
    }
	$('.content p img').attr('height','');		
});

//新增小站访问数
function addView() {
	
	var arr = new Array();
	var type = $('#data-bar').attr('data-type');
	var id = $('#data-bar').attr('data-id');
	if(!type || !id){
		return false;	
	}
	var cookeName = 'hua@'+type+'_addview';
	var str = $.cookie(cookeName) ? $.cookie(cookeName) : 0;
	if (str) {
		arr = str.split(',');
	}
	if ($.inArray(id, arr) > 0) {
		return true;
	} else {
		var val = str + ',' + id;
		$.cookie(cookeName, val);
		$.get('/?c=Ajax&a='+type+'&do=addview&id=' + id);
		return false;
	}
}
